﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Web;
namespace Common.Net
{
    /// <summary>
    /// 实现会话功能的WebClient类
    /// </summary>
    public class HttpWebClient : WebClient
    {
        private CookieContainer cookies;

        public HttpWebClient()
        {
            this.cookies = new CookieContainer();
        }

        public HttpWebClient(CookieContainer cookies)
        {
            this.cookies = cookies;
        }

        public CookieContainer Cookies
        {
            get
            {
                return this.cookies;
            }
            set
            {
                this.cookies = value;
            }
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);

            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.CookieContainer = cookies;
            }
            return request;
        }
        /// <summary>
        /// 向一个(完整的)地址post一个对象，这个对象必须是Serializable的,还可以通过它来发送文件，把文件打成字节流放入OBJECT中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bytes"></param>
        public void PostDataToURL(string url, Object obj)
        {
            if (obj == null) return;
            BinaryFormatter bFormatter = new BinaryFormatter();
            MemoryStream mStream = new MemoryStream();
            bFormatter.Serialize(mStream, obj);
            byte[] bytes = mStream.ToArray();
            mStream.Flush();
            mStream.Close();
            mStream.Dispose();
            UploadData(url, "POST", bytes);
        }

        /// <summary>
        /// 向一个(完整的)地址post一个字符串(不同网站可能会有不同的编码格式，这里选用UTF8)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bytes"></param>
        public string PostDataToURL(string url, string data)
        {
            if (string.IsNullOrEmpty(data)) { return ""; }
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
            byte[] pageData = UploadData(url, "POST", byteArray);
            string str = System.Text.Encoding.UTF8.GetString(pageData);
            return str;
        }

        /// <summary>
        /// 向一个(完整的)地址post一个字符串(不同网站可能会有不同的编码格式，这里选用UTF8)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bytes"></param>
        public void PostData(string url, string data)
        {
            if (string.IsNullOrEmpty(data)) { return; }
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
            UploadData(url, "POST", byteArray);
        }

        /// <summary>
        /// 模仿IE浏览器访问网页
        /// </summary>
        /// <param name="rurl"></param>
        public CookieContainer PostDataLikeIE(string rurl, List<KeyValuePair<string, string>> lkv, out byte[] bytes)
        {
            CookieContainer cc = new CookieContainer();
            HttpWebClient hwc = new HttpWebClient(cc);
            System.Collections.Specialized.NameValueCollection form = new System.Collections.Specialized.NameValueCollection();
            hwc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727;)");
            for (int i = 0; i < lkv.Count; i++)
            {
                form.Add(lkv[i].Key, lkv[i].Value);
            }
            bytes = hwc.UploadValues(rurl, form);
            return hwc.cookies;
        }

    }
}
